using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CurveFlying : MonoBehaviour
{
    public AnimationCurve Curve;
    public float MaxHeight = 5f;
    public float TotalTime = 3f;
    public Vector3 EndPosition;
    private Vector3 _startPosition;

    private void Start()
    {
        _startPosition = transform.position;
    }

    public void StartFly()
    {
        StopAllCoroutines();
        StartCoroutine(FlyWithCurve(Curve));

    }

    IEnumerator FlyWithCurve(AnimationCurve curve)
    {
        float time = 0;
        Vector3 dir = (EndPosition - _startPosition).normalized;
        float distance = Vector3.Distance(EndPosition, _startPosition);
        while (time < TotalTime)
        {
            float normalizedTime = time / TotalTime;
            float sampleRate = curve.Evaluate(normalizedTime);
            transform.position = _startPosition + dir * (distance * normalizedTime) + (sampleRate * MaxHeight) * Vector3.up;
            time += Time.deltaTime;
            yield return null;
        }
    }


}
